package com.zby.simulationHibernate.util.factory;

import com.zby.simulationHibernate.constant.StandardBasicTypes;
import com.zby.simulationHibernate.util.exception.GenericException;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

/**
 * Created By zby on 17:05 2019/1/28
 */

public class TransformFactory<T> {

    private TransformFactory() {
    }

    public static TransformFactory createTransformFactory() {
        return new TransformFactory();
    }

    /**
     * 数据类型转换
     *
     * @param clazz      当前类变量
     * @param obj        实例化对象
     * @param methodName 方法名
     * @param resultSet  数据的结果集
     * @param columnName 字段名
     * @param paramType  形参类型
     * @throws NoSuchMethodException
     * @throws IllegalAccessException
     * @throws InvocationTargetException
     */
    public void transformType(Class<T> clazz, T obj, ResultSet resultSet, String columnName, String methodName, Type paramType) {
        Method method = null;
        try {
            if (StandardBasicTypes.LONG.equalsIgnoreCase(paramType.toString())) {
                method = clazz.getMethod(methodName, Long.class);
                method.setAccessible(true);
                method.invoke(obj, resultSet.getLong(columnName));
            } else if (StandardBasicTypes.STRING.equalsIgnoreCase(paramType.toString())) {
                method = clazz.getMethod(methodName, String.class);
                method.setAccessible(true);
                method.invoke(obj, resultSet.getString(columnName));
            } else if (StandardBasicTypes.INTEGER.equalsIgnoreCase(paramType.toString())) {
                method = clazz.getMethod(methodName, Integer.class);
                method.setAccessible(true);
                method.invoke(obj, resultSet.getInt(columnName));
            } else if (StandardBasicTypes.BIG_DECIMAL.equalsIgnoreCase(paramType.toString())) {
                method = clazz.getMethod(methodName, BigDecimal.class);
                method.setAccessible(true);
                method.invoke(obj, resultSet.getBigDecimal(columnName));
            } else if (StandardBasicTypes.DOUBLE.equalsIgnoreCase(paramType.toString())) {
                method = clazz.getMethod(methodName, Double.class);
                method.setAccessible(true);
                method.invoke(obj, resultSet.getDouble(columnName));
            } else if (StandardBasicTypes.FLOAT.equalsIgnoreCase(paramType.toString())) {
                method = clazz.getMethod(methodName, Float.class);
                method.setAccessible(true);
                method.invoke(obj, resultSet.getFloat(columnName));
            } else if (StandardBasicTypes.DATE.equalsIgnoreCase(paramType.toString())) {
                method = clazz.getMethod(methodName, Date.class);
                method.setAccessible(true);
                method.invoke(obj, resultSet.getTimestamp(columnName));
            } else {
                System.out.println(paramType);
                throw new GenericException("数据转型失败");
            }
        } catch (SQLException e) {
            System.out.println("结果集异常resultSet=" + resultSet);
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            System.out.println("非法访问异常");
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            System.out.println("调用目标异常");
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            System.out.println("没有当前这个方法methodName=" + methodName);
            e.printStackTrace();
        }
    }
}
